﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CodeAuto.DALS
{
    public class FileDALS
    {
        public static string GetModelString(Models.T_Project project, Models.T_Table table, List<Models.T_Field> fields)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("namespace " + project.ProjectName + ".Models");
            sb.AppendLine("{");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// " + table.Description + "");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    public class " + table.TableName + "");
            sb.AppendLine("    {");
            for (int i = 0; i < fields.Count; i++)
            {
                sb.AppendLine("        /// <summary>");
                sb.AppendLine("        /// " + fields[i].FieldName + "");
                sb.AppendLine("        /// </summary>");
                sb.AppendLine("        public " + fields[i].Attr + "  " + fields[i].Field + " { get; set; }");
                sb.AppendLine("");

                if (fields[i].Field.EndsWith("Id") && fields[i].Field.Length > 2)
                {
                    sb.AppendLine("        /// <summary>");
                    sb.AppendLine("        /// 相关" + fields[i].FieldName.Replace("编号", "") + "");
                    sb.AppendLine("        /// </summary>");
                    sb.AppendLine("        public virtual " + fields[i].Field.Replace("Id", "") + "  " + fields[i].Field.Replace("Id", "") + " { get; set; }");
                    sb.AppendLine("");
                }
                sb.AppendLine("");
            }
            sb.AppendLine("        public " + table.TableName + "(){" + "");
            for (int i = 0; i < fields.Count; i++)
            {
                if (fields[i].Attr.Equals("int"))
                {
                    sb.AppendLine("                this." + fields[i].Field + "=int.MinValue;");
                }
            }

            sb.AppendLine("        }");
            sb.AppendLine("     }");
            sb.AppendLine("}");
            return sb.ToString();
        }


        public static string GetInterfaceString(Models.T_Project project, Models.T_Table table)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using " + project.ProjectName + ".Models;");
            sb.AppendLine("namespace " + project.ProjectName + ".Interfaces");
            sb.AppendLine("{");
            sb.AppendLine("        public interface I" + table.TableName + "");
            sb.AppendLine("        {");

            sb.AppendLine("                #region 默认接口");

            sb.AppendLine("                /// <summary>");
            sb.AppendLine("                /// 得到所有 " + table.Description + "在大项目中尽量少用，这个方法返回的数据量过于庞大");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <returns>所有" + table.Description + "</returns>" + "");
            sb.AppendLine("                List<" + table.TableName + "> GetList();");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 分页显示" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"PageSize\">每页记录数</param>" + "");
            sb.AppendLine("                /// <param name=\"NowPage\">当前页</param>" + "");
            sb.AppendLine("                /// <param name=\"PageCount\">总共多少页</param>" + "");
            sb.AppendLine("                /// <param name=\"recordCount\">总记录数</param>" + "");
            sb.AppendLine("                /// <returns>查询到的记录</returns>" + "");
            sb.AppendLine("                List<" + table.TableName + "> GetListByPage(int PageSize, int NowPage, out int PageCount, out int recordCount);");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 分页模糊查询" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <param name=\"PageSize\">每页记录数</param>" + "");
            sb.AppendLine("                /// <param name=\"NowPage\">当前页</param>" + "");
            sb.AppendLine("                /// <param name=\"PageCount\">总共多少页</param>" + "");
            sb.AppendLine("                /// <param name=\"recordCount\">总记录数</param>" + "");
            sb.AppendLine("                /// <returns>查询到的记录</returns>" + "");
            sb.AppendLine("                List<" + table.TableName + "> GetListByPageLikeKey(" + table.TableName + " " + table.TableName.ToLower() + ",int PageSize, int NowPage, out int PageCount, out int recordCount);");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 分页查询" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <param name=\"PageSize\">每页记录数</param>" + "");
            sb.AppendLine("                /// <param name=\"NowPage\">当前页</param>" + "");
            sb.AppendLine("                /// <param name=\"PageCount\">总共多少页</param>" + "");
            sb.AppendLine("                /// <param name=\"recordCount\">总记录数</param>" + "");
            sb.AppendLine("                /// <returns>查询到的记录</returns>" + "");
            sb.AppendLine("                List<" + table.TableName + "> GetListByPageAndKeys(" + table.TableName + " " + table.TableName.ToLower() + ",int PageSize, int NowPage, out int PageCount, out int recordCount);");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 得到一个" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"id\">编号</param>" + "");
            sb.AppendLine("                /// <returns>对象</returns>" + "");
            sb.AppendLine("                " + table.TableName + " GetModel(int id);");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 保存一个" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>编号</returns>" + "");
            sb.AppendLine("                int Insert(" + table.TableName + " " + table.TableName.ToLower() + ");");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 按关键字查询头几条" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"count\">数量</param>" + "");
            sb.AppendLine("                /// <returns>" + table.Description + "的列表</returns>" + "");
            sb.AppendLine("                List<" + table.TableName + "> GetTopList(int count);");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 判断" + table.Description + "是否唯一存在" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>是否存在</returns>" + "");
            sb.AppendLine("                bool IsExist(" + table.TableName + " " + table.TableName.ToLower() + ");");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 判断" + table.Description + "是否存在" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>是否存在</returns>" + "");
            sb.AppendLine("                bool IsJustExist(" + table.TableName + " " + table.TableName.ToLower() + ");");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 得到关键字等于。。。的所有" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>" + table.Description + "的List</returns>" + "");
            sb.AppendLine("                List<" + table.TableName + "> GetSomeObject(" + table.TableName + " " + table.TableName.ToLower() + ");");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 编辑一个" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                void Edit(" + table.TableName + " " + table.TableName.ToLower() + ");");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 删除一个" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                void Delete(" + table.TableName + " " + table.TableName.ToLower() + ");");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 删除所有" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                void DeleteAll();");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 按关键字删除" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                void DeleteColumByKey_Value(" + table.TableName + " " + table.TableName.ToLower() + ");");

            sb.AppendLine("                #endregion");

            sb.AppendLine("                #region 自定义接口");


            sb.AppendLine("                #endregion");

            sb.AppendLine("        }");

            sb.AppendLine("}");

            return sb.ToString();
        }


        public static string GetImplString(Models.T_Project project, Models.T_Table table)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using " + project.ProjectName + ".Models;");
            sb.AppendLine("using " + project.ProjectName + ".Interfaces;");
            sb.AppendLine("namespace " + project.ProjectName + ".Impls");
            sb.AppendLine("{");
            sb.AppendLine("        public class Impl" + table.TableName + ":I" + table.TableName + "");
            sb.AppendLine("        {");
            sb.AppendLine("                private DavidDB.ADO.DBManager dbmanager=null;");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("                #region 返回接口实现");
            sb.AppendLine("                private Impl" + table.TableName + "(DavidDB.ADO.DBManager db){this.dbmanager=db;}");
            sb.AppendLine("                public static I" + table.TableName + " GetInstance(DavidDB.ADO.DBManager db){");
            sb.AppendLine("                        I" + table.TableName + " instance=new Impl" + table.TableName + "(db);");
            sb.AppendLine("                        return instance;");
            sb.AppendLine("                }");

            sb.AppendLine("                #endregion");


            sb.AppendLine("                #region 默认接口实现");

            sb.AppendLine("                /// <summary>");
            sb.AppendLine("                /// 得到所有 " + table.Description + "在大项目中尽量少用，这个方法返回的数据量过于庞大");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <returns>所有" + table.Description + "</returns>" + "");
            sb.AppendLine("                public List<" + table.TableName + "> GetList(){");
            sb.AppendLine("                     ");
            sb.AppendLine("                    " + table.TableName + " field = new " + table.TableName + "();" + "");
            sb.AppendLine("                    var objs = dbmanager.GetList(field);" + "");
            sb.AppendLine("                    List<" + table.TableName + "> fields = new List<" + table.TableName + ">();" + "");
            sb.AppendLine("                    foreach (" + table.TableName + " _field in objs)" + "");
            sb.AppendLine("                    {" + "");
            sb.AppendLine("                        fields.Add(_field);" + "");
            sb.AppendLine("                    }" + "");
            sb.AppendLine("                    return fields;" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 分页显示" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"PageSize\">每页记录数</param>" + "");
            sb.AppendLine("                /// <param name=\"NowPage\">当前页</param>" + "");
            sb.AppendLine("                /// <param name=\"PageCount\">总共多少页</param>" + "");
            sb.AppendLine("                /// <param name=\"recordCount\">总记录数</param>" + "");
            sb.AppendLine("                /// <returns>查询到的记录</returns>" + "");
            sb.AppendLine("                public List<" + table.TableName + "> GetListByPage(int PageSize, int NowPage, out int PageCount, out int recordCount){");
            sb.AppendLine("                    " + table.TableName + " field = new " + table.TableName + "();" + "");
            sb.AppendLine("                    var objs = dbmanager.GetListByPage(PageSize, NowPage, out PageCount, out recordCount, field);" + "");
            sb.AppendLine("                    List<" + table.TableName + "> fields = new List<" + table.TableName + ">();" + "");
            sb.AppendLine("                    foreach (" + table.TableName + " _field in objs)" + "");
            sb.AppendLine("                    {" + "");
            sb.AppendLine("                        fields.Add(_field);" + "");
            sb.AppendLine("                    }" + "");
            sb.AppendLine("                    return fields;" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 分页模糊查询" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <param name=\"PageSize\">每页记录数</param>" + "");
            sb.AppendLine("                /// <param name=\"NowPage\">当前页</param>" + "");
            sb.AppendLine("                /// <param name=\"PageCount\">总共多少页</param>" + "");
            sb.AppendLine("                /// <param name=\"recordCount\">总记录数</param>" + "");
            sb.AppendLine("                /// <returns>查询到的记录</returns>" + "");
            sb.AppendLine("                public List<" + table.TableName + "> GetListByPageLikeKey(" + table.TableName + " " + table.TableName.ToLower() + ",int PageSize, int NowPage, out int PageCount, out int recordCount){");
            sb.AppendLine("                    var objs = dbmanager.GetListByPageLikeKey(PageSize, NowPage, " + table.TableName.ToLower() + ", out PageCount, out recordCount);" + "");
            sb.AppendLine("                    List<" + table.TableName + "> fields = new List<" + table.TableName + ">();" + "");
            sb.AppendLine("                    foreach (" + table.TableName + " _field in objs)" + "");
            sb.AppendLine("                    {" + "");
            sb.AppendLine("                        fields.Add(_field);" + "");
            sb.AppendLine("                    }" + "");
            sb.AppendLine("                    return fields;" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 分页查询" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <param name=\"PageSize\">每页记录数</param>" + "");
            sb.AppendLine("                /// <param name=\"NowPage\">当前页</param>" + "");
            sb.AppendLine("                /// <param name=\"PageCount\">总共多少页</param>" + "");
            sb.AppendLine("                /// <param name=\"recordCount\">总记录数</param>" + "");
            sb.AppendLine("                /// <returns>查询到的记录</returns>" + "");
            sb.AppendLine("                public List<" + table.TableName + "> GetListByPageAndKeys(" + table.TableName + " " + table.TableName.ToLower() + ",int PageSize, int NowPage, out int PageCount, out int recordCount){");
            sb.AppendLine("                    var objs = dbmanager.GetListByPageAndKeys(PageSize, NowPage, " + table.TableName.ToLower() + ", out PageCount, out recordCount);" + "");
            sb.AppendLine("                    List<" + table.TableName + "> fields = new List<" + table.TableName + ">();" + "");
            sb.AppendLine("                    foreach (" + table.TableName + " _field in objs)" + "");
            sb.AppendLine("                    {" + "");
            sb.AppendLine("                        fields.Add(_field);" + "");
            sb.AppendLine("                    }" + "");
            sb.AppendLine("                    return fields;" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 得到一个" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"id\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>对象</returns>" + "");
            sb.AppendLine("                public " + table.TableName + " GetModel(int id){");
            sb.AppendLine("                    " + table.TableName + " field = new " + table.TableName + "();" + "");
            sb.AppendLine("                    field.Id = id;" + "");
            sb.AppendLine("                    return dbmanager.GetModel(field) as " + table.TableName + ";" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 保存一个" + table.Description + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>编号</returns>" + "");
            sb.AppendLine("                public int Insert(" + table.TableName + " " + table.TableName.ToLower() + "){");
            sb.AppendLine("                    return dbmanager.Insert(" + table.TableName.ToLower() + ");" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 按关键字查询头几条" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"count\">数量</param>" + "");
            sb.AppendLine("                /// <returns>" + table.Description + "的列表</returns>" + "");
            sb.AppendLine("                public List<" + table.TableName + "> GetTopList(int count){");
            sb.AppendLine("                    " + table.TableName + " field = new " + table.TableName + "();" + "");
            sb.AppendLine("                    var objs = dbmanager.GetTopList(count, field);" + "");
            sb.AppendLine("                    List<" + table.TableName + "> fields = new List<" + table.TableName + ">();" + "");
            sb.AppendLine("                    foreach (" + table.TableName + " _field in objs)" + "");
            sb.AppendLine("                    {" + "");
            sb.AppendLine("                        fields.Add(_field);" + "");
            sb.AppendLine("                    }" + "");
            sb.AppendLine("                    return fields;" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 判断" + table.Description + "是否唯一存在" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>是否存在</returns>" + "");
            sb.AppendLine("                public bool IsExist(" + table.TableName + " " + table.TableName.ToLower() + "){");
            sb.AppendLine("                    return dbmanager.IsExist(" + table.TableName.ToLower() + ");" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 判断" + table.Description + "是否存在" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>是否存在</returns>" + "");
            sb.AppendLine("                public bool IsJustExist(" + table.TableName + " " + table.TableName.ToLower() + "){");
            sb.AppendLine("                    return dbmanager.IsJustExist(" + table.TableName.ToLower() + ");" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 得到关键字等于。。。的所有" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                /// <returns>" + table.Description + "的List</returns>" + "");
            sb.AppendLine("                public List<" + table.TableName + "> GetSomeObject(" + table.TableName + " " + table.TableName.ToLower() + "){");
            sb.AppendLine("                    var objs = dbmanager.GetSomeObject(" + table.TableName.ToLower() + ");" + "");
            sb.AppendLine("                    List<" + table.TableName + "> fields = new List<" + table.TableName + ">();" + "");
            sb.AppendLine("                    foreach (" + table.TableName + " _field in objs)" + "");
            sb.AppendLine("                    {" + "");
            sb.AppendLine("                        fields.Add(_field);" + "");
            sb.AppendLine("                    }" + "");
            sb.AppendLine("                    return fields;" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 编辑一个" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"obj\">对象实体</param>" + "");
            sb.AppendLine("                public void Edit(" + table.TableName + " " + table.TableName.ToLower() + "){");
            sb.AppendLine("                    dbmanager.Edit(" + table.TableName.ToLower() + ");" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 删除一个" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                public void Delete(" + table.TableName + " " + table.TableName.ToLower() + "){");
            sb.AppendLine("                    dbmanager.Delete(" + table.TableName.ToLower() + ");" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 删除所有" + table.Description + "" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                public void DeleteAll(){");
            sb.AppendLine("                    " + table.TableName + " field = new " + table.TableName + "();" + "");
            sb.AppendLine("                    dbmanager.DeleteAll(field);" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                /// <summary>" + "");
            sb.AppendLine("                /// 按关键字删除" + "");
            sb.AppendLine("                /// </summary>" + "");
            sb.AppendLine("                /// <param name=\"" + table.TableName.ToLower() + "\">对象实体</param>" + "");
            sb.AppendLine("                public void DeleteColumByKey_Value(" + table.TableName + " " + table.TableName.ToLower() + "){");
            sb.AppendLine("                     dbmanager.DeleteColumByKey_Value(" + table.TableName.ToLower() + ");" + "");
            sb.AppendLine("                }");

            sb.AppendLine("                #endregion");

            sb.AppendLine("                #region 自定义接口实现");


            sb.AppendLine("                #endregion");

            sb.AppendLine("             }");
            sb.AppendLine("        }");

            return sb.ToString();
        }
    }
}